<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Carts extends Base{
	//加入购物车
	public function cartIn()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
		if(input("param.type")==1||input("param.type")==2){
			$data['type']=input("param.type");
		}else{
			return json(['code' => 502, 'msg' => '产品还是门票']);
		}
		if(!empty(input("param.p_id"))){
			$data['p_id']=input("param.p_id");
		}else{
			return json(['code' => 503, 'msg' => '产品id']);
		}
		if(!empty(input("param.buy_num"))){
			$data['buy_num']=input("param.buy_num");
		}else{
			return json(['code' => 504, 'msg' => '购买数量']);
		}
        $map = [
            ['p_id', '=', $data['p_id']],
            ['state', '=', 1]
        ];
		if($data['type']==1){
			if(!empty(input("param.spec_combination_id"))){
				$data['spec_combination_id']=input("param.spec_combination_id");
        		array_push($map,['spec_combination_id','=',$data['spec_combination_id']]);
			}else{
				return json(['code' => 505, 'msg' => '规格参数id']);
			}
		}elseif($data['type']==2){
			$data['spec_combination_id']=0;
        	array_push($map,['spec_combination_id','=',$data['spec_combination_id']]);
		}
        $uid=UserCrypt::do_mdecrypt($token);
        array_push($map,['user_id','=',$uid]);
        $isIn=Db::name('carts')->where($map)->find();
        if($isIn){
			return json(['code' => 506, 'msg' => '购物车已加入过此产品']);
        }
		$data['user_id']=$uid;
		$data['state']=1;
		$data['time']=time();
		$data['uptime']=time();
		$res=Db::name('carts')->insert($data);
		if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
		}else{
            return json(['code' => 500, 'msg' => '失败！']);
		}
	}
	//我的购物车列表
	public function myCarts()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
            ['state', '=', 1]
        ];
        $uid=UserCrypt::do_mdecrypt($token);
        array_push($map,['user_id','=',$uid]);
		$res=Db::name('carts')->field('id,user_id,p_id,spec_combination_id,buy_num,type')->where($map)->order('id desc')->limit(10*($page-1),10)->select();
		if ($res) {
			foreach ($res as $key => $value) {
				if($res[$key]['type']==1){
					$res[$key]['title_img']=Db::name('spec_combination')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.combination,a.num,b.title,b.img,a.money as price,b.state as p_state')->where('a.id',$res[$key]['spec_combination_id'])->find();
				}elseif($res[$key]['type']==2){
					$res[$key]['title_img']=Db::name('scenic_spot')->field('title,img,price,state as p_state')->where('id',$res[$key]['p_id'])->find();
                    $res[$key]['title_img']['combination']='(景区门票)';
                    $res[$key]['title_img']['num']=0;
				}
			}
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
		}else{
            return json(['code' => 500, 'msg' => '查无数据！']);
		}
	}
	//取消(删除)我的购物车
	public function cancelCart()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=explode(',', input("param.id"));
        }else{
            return json(['code' => 502, 'msg' => 'id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res= Db::name('carts')->where(array('id'=>$id,'user_id'=>$uid,'state'=>1))->update(['state'=>2]);
        if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
		}else{
            return json(['code' => 500, 'msg' => '失败！']);
		}
	}
}